<?php


namespace app\api\controller\v1;
use app\api\model\v1\MechanismCourse;
use app\api\model\v1\NoticeActicle;
use app\api\model\v1\UserCheck;
use app\api\model\v1\UserTeacher;
use app\common\controller\Api;


class Courses  extends Api
{
    protected $noNeedLogin = ['mechanismList','teacherList','mechanismNoticeList','mechanismIndexCoursesList'];
    protected $noNeedRight = ['*'];

    /**
     * 职业课程-机构列表
     */
    public function mechanismList()
    {
        $limit = $this->request->post('limit') ?? 10;
        $category_id = $this->request->post('category_id'); //分类ID
        $map = $map2 = [];
        if ($category_id) {
            $map2 = "FIND_IN_SET($category_id,courses_category_ids)";
        }
        $map['status'] = ['=', 1];
        $list = UserCheck::with(['user'=>function($query) {
            $query->field('id,avatar');
        }])->where($map)->where($map2)->order('id desc')->paginate($limit)->toArray();
        $this->success('成功', $list);
    }

    /**
     * 职业课程-老师列表
     */
    public function teacherList()
    {
        $limit = $this->request->post('limit') ?? 10;

        $category_id = $this->request->post('category_id'); //分类ID
        $is_home = $this->request->post('is_home',''); //分类ID

        $map = $map2 = [];
        if ($category_id) {
            $map2 = "FIND_IN_SET($category_id,courses_category_ids)";
        }
        $map['status'] = ['=', 1];
        if($is_home!=''){
            $map['is_home'] = ['=', $is_home];
        }

        $list = UserTeacher::where($map)->where($map2)->order('id desc')->paginate($limit)->toArray();
        $this->success('成功', $list);
    }

    /**
     * 机构新闻资讯
     */
    public function mechanismNoticeList()
    {
        $id = $this->request->post('id'); //机构ID
        $category_id = $this->request->post('category_id'); //分类ID
        $limit = $this->request->post('limit') ?? 10;
        $map = [];
        $map['user_check_id'] = ['=', $id];
        if ($category_id) {
            $map['notice_category_id'] = ['=', $category_id];
        }
        $list = NoticeActicle::where($map)->order('id desc')->paginate($limit)->toArray();
        $this->success('成功', $list);
    }

    /**
     * 机构推荐课程
     */
    public function mechanismIndexCoursesList()
    {
        $limit = $this->request->post('limit') ?? 10;
        $id = $this->request->post('id'); //机构ID
        $category_id = $this->request->post('category_id'); //分类ID
        $mechanism_category_topid = $this->request->post('mechanism_category_topid'); //专业ID
        $mechanism_category_id = $this->request->post('mechanism_category_id'); //级别ID
        $checkModel = UserCheck::where('id', $id)->find();
        if (!$checkModel) $this->error('机构不存在');
        $map = [];
        if ($category_id) {
            $map['pid'] = ['=', $category_id];
        }
        if ($mechanism_category_topid) {
            $map['mechanism_category_topid'] = ['=', $mechanism_category_topid];
        }
        if ($mechanism_category_id) {
            $map['mechanism_category_id'] = ['=', $mechanism_category_id];
        }
        $map['is_index'] = ['=', 1];
        $map['user_check_id'] = ['=', $id];
        $list = MechanismCourse::where($map)->orderRaw('rand()')->paginate($limit)->toArray();
        $this->success('成功', $list);
    }
}